package com.lagou.edu.course.remote;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.course.dto.CourseDTO;
import com.lagou.edu.course.entity.Course;
import com.lagou.edu.course.param.CourseQueryParam;
import com.lagou.edu.course.service.ICourseService;
import com.lagou.edu.message.entity.Message;
import com.lagou.edu.message.remote.MessageService;
import com.lagou.edu.util.ConverUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 程  林
 * @date 2021-01-31 16:49
 * @description //
 * @since V1.0.0
 */
@RestController
public class CourseRemoteService implements CourseService {


    @Autowired
    private ICourseService courseService;


    @Override
    @PostMapping("/course/queryCourseList")
    public Page<CourseDTO> queryCourseList(@RequestBody CourseQueryParam courseQueryParam) {
        List<CourseDTO> records = new ArrayList<>();


        Integer pageSize = courseQueryParam.getPageSize();
        Integer currentPage = courseQueryParam.getCurrentPage();
        Page<Course> page = new Page<>(currentPage, pageSize);

        String courseName = courseQueryParam.getCourseName();
        Integer status = courseQueryParam.getStatus();
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(courseName)) {
            queryWrapper.like("course_name", courseName);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        IPage<Course> coursePage = courseService.getBaseMapper().selectPage(page, queryWrapper);
        coursePage.getRecords().forEach(data -> {
            CourseDTO dto = new CourseDTO();
            BeanUtils.copyProperties(data, dto);
            records.add(dto);
        });

        //分页查询结果对象属性的拷贝
        Page<CourseDTO> result = new Page<>();
        BeanUtils.copyProperties(coursePage, result);

        //设置分页结果对象record属性
        result.setRecords(records);
        result.setTotal(coursePage.getTotal());
        return result;

    }

    @Override
    @PostMapping("/course/addOrUpdateCourse")
    public Boolean addOrUpdateCourse(@RequestBody CourseDTO courseDTO) {

        Course convert = ConverUtil.convert(courseDTO, Course.class);

        if (convert == null) {
            return false;
        }
        Date now = new Date();
        if (convert.getId() != null) {
            convert.setUpdateTime(now);
        } else {
            convert.setCreateTime(now);
            convert.setUpdateTime(now);
        }

        boolean save = false;
        try {
            save = courseService.saveOrUpdate(convert);
        } catch (Exception e) {
            e.printStackTrace();
            return save;
        }

        return save;
    }

    @Autowired
    private MessageService remoteMessageService;

    @Override
    @PostMapping("/course/updateCourseState")
    public Object updateCourseState(CourseDTO courseDTO) {

        Course convert = ConverUtil.convert(courseDTO, Course.class);

        if (convert == null || convert.getId() == null) {
            return false;
        }


        Date now = new Date();
        convert.setUpdateTime(now);
        Integer status = convert.getStatus();
        // 1.上架；0下架
        if (1 == status) {
            Message message = new Message();
            message.setCourseId(convert.getId());
            message.setRemark("上架");

            remoteMessageService.sendMsg(message);

        }
        boolean update = false;
        try {
            update = courseService.updateById(convert);
        } catch (Exception e) {
            e.printStackTrace();
            return update;
        }

        return update;
    }
}
